package com.ay.wiki_plt.controller;

import com.alibaba.fastjson.JSONObject;
import com.ay.wiki_plt.common.PageBean;
import com.ay.wiki_plt.common.Result;
import com.ay.wiki_plt.domain.dto.PageRequestDTO;
import com.ay.wiki_plt.domain.dto.UserLoginReq;
import com.ay.wiki_plt.domain.dto.UserResetPasswordReq;
import com.ay.wiki_plt.domain.dto.UserSaveReq;
import com.ay.wiki_plt.domain.vo.UserLoginResp;
import com.ay.wiki_plt.service.UserService;
import com.ay.wiki_plt.utils.SnowFlake;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private SnowFlake snowFlake;

    @Autowired
    private RedisTemplate redisTemplate;

    /*查询全部用户*/
    @GetMapping("/list")
    public Result list(@Valid PageRequestDTO pageRequestDTO,
                       @RequestParam(defaultValue = "") String name) {
        log.info("查询用户列表,模糊查询:{},{},{},", pageRequestDTO.getPage(), pageRequestDTO.getPageSize(), name);
        PageBean pageBean = userService.list(pageRequestDTO.getPage(), pageRequestDTO.getPageSize(), name);
        return Result.success(pageBean);
    }

    /*保存和修改接口*/
    @PostMapping
    public Result save(@Valid @RequestBody UserSaveReq req) {
        log.info("保存和修改接口参数:{}", req);
        //对密码进行32位的16进制 md5加密
        req.setPassword(DigestUtils.md5DigestAsHex(req.getPassword().getBytes()));
        userService.save(req);
        return Result.success();
    }

    /*批量删除接口*/
    @DeleteMapping("/{ids}")
    public Result deleteBatch(@PathVariable List<Long> ids) {
        log.info("删除的用户id:{}", ids);
        userService.deleteBath(ids);
        return Result.success();
    }

    /*重置密码接口*/
    @PostMapping("/reset-password")
    public Result resetPassword(@Valid @RequestBody UserResetPasswordReq req) {
        log.info("重置密码接口参数:{}", req);
        userService.resetPassword(req);
        return Result.success();
    }


    /*登录接口*/
    @PostMapping("/login")
    public Result login(@Valid @RequestBody UserLoginReq req) {
        //对数据进行MD5加密
        req.setPassword(DigestUtils.md5DigestAsHex(req.getPassword().getBytes()));
        log.info("登录接口参数:{}", req);
        UserLoginResp userLoginResp = userService.login(req);


        //登录成功生成token
        Long token = snowFlake.nextId();
        log.info("生成单点登录token:{}，并放入redis中", token);
        userLoginResp.setToken(token.toString());
        //使用redis进行存储

        redisTemplate.opsForValue().set(token.toString(), JSONObject.toJSONString(userLoginResp), 3600 * 24, TimeUnit.SECONDS);
        return Result.success(userLoginResp);
    }


    @GetMapping("/logout/{token}")
    public Result logout(@PathVariable String token) {
        redisTemplate.delete(token);
        log.info("从redis中删除token:{}", token);
        return Result.success();
    }

}
